<!DOCTYPE html>
<html class="ui-page-login">

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title></title>
		<link href="../css/mui.min.css" rel="stylesheet" />
		<link href="../css/style.css" rel="stylesheet" />
		<style>
			.area {
				margin: 20px auto 0px auto;
			}
			.mui-input-group:first-child {
				margin-top: 20px;
			}
			.mui-input-group label {
				width: 22%;
			}
			.mui-input-row label~input,
			.mui-input-row label~select,
			.mui-input-row label~textarea {
				width: 78%;
			}
			.mui-checkbox input[type=checkbox],
			.mui-radio input[type=radio] {
				top: 6px;
			}
			.mui-content-padded {
				margin-top: 25px;
			}
			.mui-btn {
				padding: 10px;
			}
			
		</style>
	</head>

	<body ng-app="mainApp" ng-controller="regCtrl" >
		<header class="mui-bar mui-bar-nav mui-bar-detail">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title">注册</h1>
		</header>
		<div class="mui-content reg_con ">
			<form class="mui-input-group">
				<div class="mui-input-row">
					<label>账号</label>
					<input id='userName' name='userName' ng-model="user.userName"  type="text" class="mui-input-clear mui-input" placeholder="请输入账号(英文和数字)" ng-change="userNamechange()" >
				</div>
				<div class="mui-input-row">
					<label>密码</label>
					<input id='password' name='password' type="password" ng-model="user.password"  class="mui-input-clear mui-input" placeholder="请输入密码">
				</div>
				<div class="mui-input-row">
					<label>确认</label>
					<input id='password_confirm' name='password_confirm' type="password" ng-model="password_confirm"  class="mui-input-clear mui-input" placeholder="请确认密码">
				</div>
				<div class="mui-input-row">
					<label>公司名称</label>
					<input id='companyName' name='companyName' type="text" ng-model="user.companyName" class="mui-input-clear mui-input" placeholder="请输入公司名称">
				</div>
				<div class="mui-input-row">
					<label>信用代码</label>
					<input id='xydm' name='xydm' type="text" ng-model="user.xydm"  class="mui-input-clear mui-input" placeholder="请输入信用代码">
				</div>
				<div class="mui-input-row">
					<label>代理商</label>
					<select class="mui-h5" style="margin: auto;" ng-model="user.agentCompany">
						<option value="">--请选择代理--</option>
						<option ng-repeat="agent in agentList" value="{{agent.id}}">{{agent.agentCompany}}</option>
					</select>
				</div>
				<div class="mui-input-row ">
					<label>法人</label>
					<input id='realName' name='realName' ng-model="user.realName" type="text" class="mui-input-clear mui-input" placeholder="请输入法人">
				</div>
				<div class="mui-input-row feedback" style="height: 130px;">
					<label>营业执照</label>
					<input id='yyzz' name='yyzz' ng-model="user.yyzz" type="text" class="mui-input-clear mui-input" placeholder="请上传营业执照" readonly="true">
					<div id='image-list' class="row image-list"></div>
				</div>
				<div class="mui-input-row">
					<label>电话</label>
					<input id='phone' name='phone' ng-model="user.phone"    type="tel" class="mui-input-clear mui-input" placeholder="请输入联系方式">
				</div>
				<div class="mui-input-row code">
					<label>验证码</label>
					<input id='yzm' name='yzm' ng-model="yzm" type="text"  class="mui-input-clear mui-input " placeholder="请输入验证码">
					<span class="mui-btn mui-btn-success" id="getYZM" >
					获取验证码
				</span>
				</div>
			</form>
			<div class="mui-input-row mui-checkbox mui-left agreement">
				<label><a href="#" id='xyContent'>《同意并阅读协议》</a></label>
				<input name="checkbox" value="" type="checkbox" checked="">
			</div>
			<div class="mui-content-padded">
				<button id='reg' class="mui-btn mui-btn-block mui-btn-primary reg">注册</button>
			</div>
			<div class="mui-content-padded">
				<p>注册后的用户可根据系统返回的注册码查验受否注册成功，注册成功后的用户可根据用户名密码直接登陆。</p>
			</div>
		</div>
		
		<!--common---start-->
		<script src="../js/angular/angular.min.js"></script>
		<script src="../js/angular/angular-resource.js"></script>
		<script src="../js/angular/common.js"></script>
		<!--common---end-->
		<script src="../js/mui.min.js"></script>
		<script src="../js/common.js"></script>
		<script src="../js/app.js"></script>
		<script src="../js/feedback-page.js" type="text/javascript" charset="utf-8"></script>
		<script>
				mainApp.controller('regCtrl', function($scope, $http) {
					
					(function($, doc) {
					$.init();
					$.plusReady(function() {
						var self = plus.webview.currentWebview();
						var ifUpdate=self.ifUpdate;
						var users = JSON.parse(localStorage.getItem('$users') || '[]');
						var checkStatus;
						var settings = app.getSettings();
						var regButton = doc.getElementById('reg');
						var userNameBox = doc.getElementById('userName');
						var passwordBox = doc.getElementById('password');
						var xydmBox = doc.getElementById('xydm');
						var companyNameBox = doc.getElementById('companyName');
						var passwordConfirmBox = doc.getElementById('password_confirm');
						var realNameBox = doc.getElementById('realName');
						var phoneBox = doc.getElementById('phone');
						var xyContentBox = doc.getElementById('xyContent');
						var xieyiBox= doc.getElementById('xieyi');
						var getYZM=doc.getElementById("getYZM");
						users.some(function(user) {
							checkStatus = user.checkStatus;
							
						});
						$scope.user={};
						$scope.yzmNumber="";
						$scope.pic_path="";
						 
						 if(ifUpdate=='update'){
						 	if(checkStatus=='2'){//审核未通过，这是来修改信息来了
								users.some(function(user) {
									document.querySelector('input[name="userName"]').value=user.userName;
									//document.querySelector('input[name="password"]').value=user.password;
									//document.querySelector('input[name="password_confirm"]').value=user.password;
									document.querySelector('input[name="realName"]').value=user.realName;
									//document.querySelector('input[name="phone"]').value=user.phone;
									document.querySelector('input[name="xydm"]').value=user.xydm;
									document.querySelector('input[name="companyName"]').value=user.companyName;
									$scope.user.userName=user.userName;
									$scope.user.companyName=user.companyName;
									$scope.user.xydm=user.xydm;
									$scope.user.realName=user.realName;
									$scope.user.registCode=user.registCode;
									$scope.user.agentCompany=user.agentId;
								});
							}
						 }
						
						
						regButton.addEventListener('tap', function(event) {
							var formData = new FormData();
		                    if(document.querySelector('input[type="file"]').files[0]!=null){
		                        formData.append("file",document.querySelector('input[type="file"]').files[0]);
		                    }else{
		                        mui.alert("请选择文件");
		                    }
							uploadFile(formData);
							
							
						});
						xyContentBox.addEventListener('tap', function(event) {
							mui.openWindow({
								url:'xieyi.html',
								id:'xieyi',
								extras:{//参数传递
									xyType:'yhxy'
								}
							});
						});
						
						//获取验证码
						getYZM.addEventListener('tap', function(event) {
							var phone=phoneBox.value;
							if(phone==undefined||phone==""){
								mui.alert("请输入手机号");
							}else{
								mui.ajax(baseUrl+"/dapi/phone/sendYZM/"+phone,{
								    dataType:'json',//服务器返回json格式数据
								    type:'get',//HTTP请求类型	
								    timeout:10000,//超时时间设置为10秒；
								    headers:{'Content-Type':'application/json'},                  
								    success:function(data){
								    	$scope.yzmNumber=data.data;
								    	if(data.status=='1'){
								    		mui.alert("已发送")
								    	}else{
								    		mui.alert("发送失败")
								    	}
								    },
								    error:function(err){
								    	mui.alert(err.message);
								    }
								});
							}
							
						});
						var uploadFile=function (formData) {
		                   $http({
	                            method:'POST',
	                            url:baseUrl+"/dapi/upload/link",
	                            data: formData,
	                            headers: {'Content-Type':undefined},
	                            transformRequest: angular.identity
	                        }).success(function(data){
	                            $scope.pic_path=data.data;
	                            if ($scope.password_confirm != $scope.user.password) {
								plus.nativeUI.toast('密码两次输入不一致');
								return;
								}
								if ($scope.user.phone==undefined||$scope.user.phone=="") {
									plus.nativeUI.toast('请输入手机号码');
									return;
								}
								if ($scope.yzm==undefined||$scope.yzm=="") {
									plus.nativeUI.toast('请先输入验证码');
									return;
								}
								if ($scope.yzmNumber!=$scope.yzm) {
									plus.nativeUI.toast('验证码输入错误');
									return;
								}
								if ($scope.user.agentCompany==undefined||$scope.user.agentCompany=="") {
									plus.nativeUI.toast('请选择代理公司');
									return;
								}
								if ($scope.pic_path==undefined||$scope.pic_path=="") {
									plus.nativeUI.toast('请先上传营业执照');
									return;
								}else{
									$scope.user.yyzz=$scope.pic_path;
								} 
								if(checkStatus=='2'&&ifUpdate=='update'){
									app.updateReg($scope.user, function(err) {
										if (err) {
											plus.nativeUI.toast(err);
											return;
										}
									
									});
								}else{
									app.reg($scope.user, function(err) {
										if (err) {
											plus.nativeUI.toast(err);
											return;
										}
									
									});
								}
	                        });
		                };
		                //获取代理商列表
		                $http({
                            method:'GET',
                            url:baseUrl+"/dapi/agent/getList",
                            headers: {'Content-Type':undefined},
                            transformRequest: angular.identity
                        }).success(function(data){
                        	$scope.agentList=data.data;
                        });
		                $scope.userNamechange=function(){
		                	document.querySelector('input[name="userName"]').value=document.querySelector('input[name="userName"]').value.replace(/[\W]/g,'');
		                }
		                
		                
					});
				}(mui, document));
				//上传图片
		               
				
				});
			
		</script>
	</body>

</html>